package uni.androidgame;

import android.graphics.Point;

import uni.androidgame.gameobjects.DungeonMap;
import uni.androidgame.gameobjects.Item;
import uni.androidgame.gameobjects.Tile;

import java.util.ArrayList;

public class Character
{
    protected int maxHp = 10;
    protected int currentHp = 10;
    protected int maxStr = 5;
    protected int currentStr = 5;
    protected int level = 1;

    private boolean _isInitialized = false;
    protected  boolean _isDead = false;

    protected Point _position;
    public Point getPosition(){return _position;}
    protected Tile currentTile;
    public Tile getCurrentTile(){return currentTile;}

    protected ArrayList<GameAction> actionQueue = new ArrayList<GameAction>();
    private String _name;

    public Character(Tile startTile,String name,int str, int hp)
    {
        _name = name;
        currentStr = str;
        currentHp = hp;
        currentTile = startTile;
        currentTile.setCharacter(this);
        _position = new Point(currentTile.getX()* DungeonMap.TileSize,currentTile.getY()*DungeonMap.TileSize);
    }
    public void setMovementActions(ArrayList<Tile> path)
    {
        for(Tile tile : path)
        {
            actionQueue.add(new GameAction(this, GameAction.Type.Move,tile,null));
        }
    }
    public void setThrowAction(Item selectedItem, Tile touchedTile)
    {
        actionQueue.clear();
        actionQueue.add(new GameAction(this, GameAction.Type.Throw,touchedTile,selectedItem));
    }
    public void setAttackAction(Tile enemyTile)
    {
        actionQueue.add(new GameAction(this, GameAction.Type.Attack,enemyTile,null));
    }

    public int getAttackPower()
    {
        //Change this to weapon power
        return currentStr+0;
    }

    public GameAction getNextAction()
    {
        if(actionQueue.size()>0)
        {
            return actionQueue.remove(0);
        }

        return null;
    }

    public void moveTo(Tile tile)
    {
        if(tile.getCharacter() ==null)
        {
            currentTile.removeCharacter();
            currentTile = tile;
            tile.setCharacter(this);
            _position.set(tile.getX()*DungeonMap.TileSize,tile.getY()*DungeonMap.TileSize);
        }
    }

    public void damage(int damage)
    {
        //Change into armor def
        int receivedDamage = damage-0;
        if(receivedDamage<0)
        {
            receivedDamage=0;
        }
        currentHp-=receivedDamage;
        GameConsole.addMessage(_name + " received " + receivedDamage + " damage");
        if(currentHp<=0)
        {
            kill();
        }
    }

    public void kill()
    {
        GameConsole.addMessage(_name + " was killed");
        _isDead= true;
        currentTile.removeCharacter();
        //Inherit
    }
}
